package cyx.example.ddd.domain.order.service;

import cyx.example.ddd.domain.order.model.aggregate.Order;
import cyx.example.ddd.domain.order.model.valueobject.Money;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Currency;

/**
 * 订单领域服务
 * 处理跨实体的业务逻辑
 */
@Service
@RequiredArgsConstructor
public class OrderDomainService {

    /**
     * 创建新订单
     */
    public Order createOrder(String userId, Currency currency) {
        return new Order(userId, currency);
    }

    /**
     * 验证订单是否可以支付
     */
    public boolean validateOrderForPayment(Order order) {
        // 订单必须有订单项
        if (order.getOrderItems().isEmpty()) {
            return false;
        }

        // 订单总金额必须大于0
        Money zero = Money.ZERO(order.getCurrency());
        return order.getTotalAmount().isGreaterThanOrEqual(zero);
    }
}